<?php

namespace Home\Model;

use Think\Model;
use Home\Util\Tools;

/**
 * 支持模型
 */
class SupportModel extends Model {

    /**
     * 发布用户的数据
     * 
     * @var array
     */
    public $user = array();

    /**
     * 梦想数据
     * 
     * @var array
     */
    public $dream = array();

    /**
     * 创建用户数据
     */
    public function createUser($id) {
        //参数验证
        if (!is_numeric($id)) {
            $this->error = '参数错误';
            return false;
        }
        $user_model = M('user');
        $dream = $user_model->find($id);
        if (empty($dream)) {
            $this->error = '数据不存在';
            return false;
        } else {
            $this->dream = $dream;
        }
        //查询是否已经支持过
        //接口查询用户信息
        import('@.Util.Tools');
        $data = array(
            'action' => 'getMyUserInfo',
            'current_uid' => $dream['user_id'],
        );
        $return = Tools::call($data);
        if (!$return) {
            $this->error = '接口调用错误';
            return false;
        }
        if ($return['result'] == 2) {
            $this->error = $return['msg'];
            return false;
        }
        $this->user = $return['data'];
		$search = array(
            'emba-user-avatar.qiniudn.com',
            'emba-dongtai.qiniudn.com',
            'emba-huodong.qiniudn.com',
            'emba-im.qiniudn.com',
            'emba-renzheng.qiniudn.com',
        );
        $replace = array(
            '7q5ay6.com1.z0.glb.clouddn.com',
            '7q5ay3.com1.z0.glb.clouddn.com',
            '7qnail.com1.z0.glb.clouddn.com',
            '7q5ay8.com1.z0.glb.clouddn.com',
            '7rylbv.com1.z0.glb.clouddn.com',
        );
        $this->user['user_icon'] = str_replace($search, $replace, $this->user['user_icon']);
        return true;
    }

    /**
     * 获取支持数
     * 
     * @param int $id 梦想ID
     * @return int
     */
    public function getSupportCount($id) {
        $support_model = M('support');
        $map = array(
            'dream_id' => $id,
            'status' => 0,
            'is_suport' => 1,
        );
        return $support_model->where($map)->count();
    }

    /**
     * 是否支持过该内容用户版
     * 
     * @param int $dream_id
     * @param string $openid
     */
    public function isSupportByUser($dream_id, $user_id) {
        $support_user_model = M('support_user');
        $map = array(
            'user_id' => $user_id,
            'status' => 0,
        );
        $user = $support_user_model->where($map)->find();
        $support_model = M('support');
        $map = array(
            'suport_user_id' => $user['suport_user_id'],
            'dream_id' => $dream_id,
            'is_support' => 1,
            'status' => 0,
        );
        $is_support = $support_model->where($map)->find();
        if ($is_support) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否支持过该内容微信版
     * 
     * @param int $dream_id
     * @param string $openid
     */
    public function isSupportByWeixin($dream_id, $openid) {
        $support_user_model = M('support_user');
        $map = array(
            'open_id' => $openid,
            'status' => 0,
        );
        $user = $support_user_model->where($map)->find();
        $support_model = M('support');
        $map = array(
            'suport_user_id' => $user['suport_user_id'],
            'dream_id' => $dream_id,
            'is_support' => 1,
            'status' => 0,
        );
        $is_support = $support_model->where($map)->find();
        if ($is_support) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取微信用户openid
     * @param string $code 授权返回码
     */
    public function getWeixinUserOpenid($code) {
        import('@.Util.Tools');
        //取access_token
        $data = array(
            'appid' => C('APP_ID'),
            'secret' => C('APP_SECRET'),
            'code' => $code,
            'grant_type' => 'authorization_code',
        );
        $return = json_decode(Tools::get('https://api.weixin.qq.com/sns/oauth2/access_token', $data), true);
        session('weixin_openid', $return['openid']);
        return $return['openid'];
    }

    /**
     * 获取微信用户信息
     * @param string $code 授权返回码
     */
    public function getWeixinUserInfo($code) {
        import('@.Util.Tools');
        //取access_token
        $data = array(
            'appid' => C('APP_ID'),
            'secret' => C('APP_SECRET'),
            'code' => $code,
            'grant_type' => 'authorization_code',
        );
        $return = json_decode(Tools::get('https://api.weixin.qq.com/sns/oauth2/access_token', $data), true);
        if ($return['errcode'] == 40029) {
            die($return['errmsg']);
        }
        //取信息
        $data = array(
            'access_token' => $return['access_token'],
            'openid' => $return['openid'],
            'lang' => 'zh_CN',
        );
        $return = json_decode(Tools::get('https://api.weixin.qq.com/sns/userinfo', $data), true);
        if ($return['errcode'] == 40003) {
            die($return['errmsg']);
        }
        return $return;
    }

    /**
     * 根据user_id创建本地数据
     * @param int $user_id
     */
    public function checkUser($user_id) {
        import('@.Util.Tools');
        $data = array(
            'action' => 'getMyUserInfo',
            'current_uid' => $user_id,
        );
        $return = Tools::call($data);
        if (!$return) {
            die('接口调用错误');
        }
        if ($return['result'] == 2) {
            die($return['msg']);
        }
        $info = $return['data'];
        //没有本地用户则创建
        $support_user_model = M('support_user');
        $map['user_id'] = $user_id;
        $map['status'] = 0;
        $support_user = $support_user_model->where($map)->find();
        if (empty($support_user)) {
            $data = array(
                'suport_user_type' => 1,
                'user_id' => $user_id,
                'suport_user_name' => $info['user_name'],
                'suport_user_icon' => $info['user_icon'],
                'other_content' => serialize($info),
                'create_date' => time(),
                'update_date' => time(),
                'status' => 0,
            );
            $support_user_model->add($data);
        } else {
            $data = array(
                'suport_user_name' => $info['user_name'],
                'suport_user_icon' => $info['user_icon'],
                'other_content' => serialize($info),
                'update_date' => time(),
            );
            $map = array(
                'user_id' => $user_id,
                'status' => 0,
            );
            $support_user_model->where($map)->save($data);
        }
        $wap_user_id = session('wap_user_id');
        if (empty($wap_user_id)) {
            //同步登录
            session('wap_user_id', $user_id);
            session('wap_user_phone', $info['user_phone']);
            session('wap_user_name', $info['user_name']);
        }
    }

}
